package com.sls.game.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.ServerDisponseException;
import com.sls.game.dao.CardExportLogDAO;
import com.sls.game.dao.CardImportLogDAO;
import com.sls.game.dao.CardInformationDAO;
import com.sls.game.pojo.CardExportLog;
import com.sls.game.pojo.CardImportLog;
import com.sls.game.pojo.CardInformation;
import com.sls.game.service.CardInformationService;

@Service("cardInformationService")
public class CardInformationServiceImpl implements CardInformationService {

	private static Logger log = Logger
			.getLogger(CardInformationServiceImpl.class);

	private CardInformationDAO cardInformationDAO;
	private CardImportLogDAO cardImportLogDAO;
	private CardExportLogDAO cardExportLogDAO;

	public CardExportLogDAO getCardExportLogDAO() {
		return cardExportLogDAO;
	}

	@Resource(name = "cardExportLogDAO")
	public void setCardExportLogDAO(CardExportLogDAO cardExportLogDAO) {
		this.cardExportLogDAO = cardExportLogDAO;
	}

	public CardImportLogDAO getCardImportLogDAO() {
		return cardImportLogDAO;
	}

	@Resource(name = "cardImportLogDAO")
	public void setCardImportLogDAO(CardImportLogDAO cardImportLogDAO) {
		this.cardImportLogDAO = cardImportLogDAO;
	}

	public CardInformationDAO getCardInformationDAO() {
		return cardInformationDAO;
	}

	@Resource(name = "cardInformationDAO")
	public void setCardInformationDAO(CardInformationDAO cardInformationDAO) {
		this.cardInformationDAO = cardInformationDAO;
	}

	private Gson gson = null;

	public CardInformationServiceImpl() {
		gson = CommonUtil.gson();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sls.game.service.CardInformationService#save(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public String save(String body, String requestId) throws Exception {
		log.info("Enter save method..." + "请求ID[" + requestId + "]");
		CardInformation info = gson.fromJson(body, CardInformation.class);
		info.setId(UUID.randomUUID().toString());
		validateEmpty(info);
		// CardInformation retMsg = cardInformationDAO.query( info.getCardId()
		// );
		// CardInformation retuMsg = cardInformationDAO.queryBySerial(
		// info.getCardSerial() );
		// if ( retMsg != null )
		// {
		// throw new ServerDisponseException( "000007", "[数据插入失败,cardId已存在！！]"
		// );
		// }
		// if ( retuMsg != null )
		// {
		// throw new ServerDisponseException( "000008",
		// "[数据插入失败,cardSerial已存在！！]" );
		// }
		CardImportLog cardImportLog = info.getCardImportLog();
		cardImportLog.setId(UUID.randomUUID().toString());
		cardImportLog.setCardBatchNum(info.getCardImportBatchNum());
		cardImportLog.setImportCardPrice(info.getCardPrice());
		cardImportLog.setImportCardMoney(info.getCardPrice()
				* cardImportLog.getImportCardNumber());
		cardImportLog.setImportCardName(info.getCardName());
		cardImportLogDAO.insert(cardImportLog);
		cardInformationDAO.insert(info);
		log.info("数据插入成功！！");
		log.info("Leave save method..." + "请求ID[" + requestId + "]");
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sls.game.service.CardInformationService#queryAll(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public String queryAll(String body, String requestId) {
		log.info("Enter queryAll method..." + "请求ID[" + requestId + "]");
		List<CardInformation> infoList = cardInformationDAO.queryAll();
		String respMsg = gson.toJson(infoList);
		log.info("Leave queryAll method..." + "请求ID[" + requestId + "]");
		return respMsg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sls.game.service.CardInformationService#queryByCardId(java.lang.String
	 * , java.lang.String)
	 */
	@Override
	public String queryByCardId(String body, String requestId) {
		log.info("Enter queryByCardId method..." + "请求ID[" + requestId + "]");
		CardInformation info = gson.fromJson(body, CardInformation.class);
		String cardId = info.getCardId();
		CardInformation cardInfo = cardInformationDAO.query(cardId);
		String respMsg = gson.toJson(cardInfo);
		log.info("Leave queryByCardId method..." + "请求ID[" + requestId + "]");
		return respMsg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sls.game.service.CardInformationService#update(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public String update(String body, String requestId) throws Exception {
		log.info("Enter update method..." + "请求ID[" + requestId + "]");
		CardInformation info = gson.fromJson(body, CardInformation.class);
		validateEmpty(info);
		cardInformationDAO.update(info);
		log.info("Leave update method..." + "请求ID[" + requestId + "]");
		return null;
	}

	/**
	 * 校验非空字段
	 * 
	 * @param info
	 * @throws ServerDisponseException
	 */
	private void validateEmpty(CardInformation info)
			throws ServerDisponseException {
		String cardId = info.getCardId();
		String cardSerial = info.getCardSerial();
		String cardType = String.valueOf(info.getCardType());
		String supplyCategory = String.valueOf(info.getSupplyCategory());
		String arsCategory = String.valueOf(info.getArsCategory());
		String regex = "\\d";
		boolean typeAllNum = cardType.matches(regex);
		boolean supCategoryAllNum = supplyCategory.matches(regex);
		boolean arsCategoryAllNum = arsCategory.matches(regex);
		if ("".equals(cardId) || null == cardId) {
			throw new ServerDisponseException("000001", "[数据插入失败,cardId为空！！]");
		} else if ("".equals(cardSerial) || null == cardSerial) {
			throw new ServerDisponseException("000002",
					"[数据插入失败,cardSerial为空！！]");
		} else if ("".equals(cardType) || null == cardType) {
			throw new ServerDisponseException("000003", "[数据插入失败,cardType为空！！]");
		} else if ("".equals(supplyCategory) || null == supplyCategory) {
			throw new ServerDisponseException("000004",
					"[数据插入失败,supplyCategory为空！！]");
		} else if ("".equals(arsCategory) || null == arsCategory) {
			throw new ServerDisponseException("000005",
					"[数据插入失败,arsCategory为空！！]");
		} else if ((!typeAllNum) || (!supCategoryAllNum)
				|| (!arsCategoryAllNum)) {
			throw new ServerDisponseException("000006", "数据插入失败,类型错误！！"
					+ "cardType为：" + cardType + "  supplyCategory为："
					+ supplyCategory + "  arsCategory为：" + arsCategory);
		} else {

		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.sls.game.service.CardInformationService#updateSaleState(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public String updateSaleState(String body, String requestId)
			throws Exception {
		log.info("Enter updateSaleState method..." + "请求ID[" + requestId + "]");
		CardInformation info1 = gson.fromJson(body, CardInformation.class);
		cardInformationDAO.updateSaleState(info1);
		CardInformation info = cardInformationDAO.query(info1.getCardId());
		CardExportLog exportLog = info1.getCardExportLog();
		exportLog.setId(UUID.randomUUID().toString());
		exportLog.setExportCardName(info.getCardName());
		exportLog.setExportCardPrice(info.getCardPrice());
		exportLog.setExportCardMoney(exportLog.getExportCardNumber()
				* info.getCardPrice());
		this.cardExportLogDAO.insert(exportLog);
		log.info("销售状态修改成功！！");
		log.info("Leave updateSaleState method..." + "请求ID[" + requestId + "]");
		return null;
	}
}
